<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Model Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/config.html
 */
class CI_Model {

	/**
	 * Constructor
	 *
	 * @access public
	 */
	 
	var $_foreign_keys = array();
	var $_parent_tables = array();
    var $table_name = '';
	var $primary_key = '';
	var $display_field = '';
	var $model_path = '';
	
	function __construct()
	{
		log_message('debug', "Model Class Initialized");
		if($this->display_field === '') {
			$this->display_field = $this->primary_key;
		}
		if($this->model_path === '') {
			$this->model_path = ucfirst($this->table_name) . '_model';
		}
	}

	/**
	 * __get
	 *
	 * Allows models to access CI's loaded classes using the same
	 * syntax as controllers.
	 *
	 * @param	string
	 * @access private
	 */
	function __get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
	
	
	function getAll($condition = array(), $select = array(), $order = array(), $option = array())
    {
        if (!empty($condition) && is_array($condition))
            foreach ($condition as $key => $value)
                $this->db->where($key, $value);

        if (!empty($order) && is_array($order))
            foreach ($order as $key => $value) {
                if (is_numeric($key)) {
                    $key = $value;
                    $value = "ASC";
                }
                $this->db->order_by($key, $value);
            }

        if (!empty($select) && is_array($select))
        {
            $s = "";
            foreach ($select as $value)
                $s .= ", $value";
            $s = substr($s, 1);
            $this->db->select($s);
        }
		
		if (!empty($option) && is_array($option))
		{
			if ($this->is_set($option, 'limit') AND $this->is_set($option, 'offset')){
				$this->db->limit($option['limit'], $option['offset']);
			}
			if ($this->is_set($option, 'group_by')){
				$this->db->group_by($option['group_by']);
			}
		}

        return $this->db->get($this->table_name)->result_array();
    }
	
    function _insert($data) {
        $this->db->insert($this->table_name, $data);
        return $this->db->insert_id();
    }

    function _update($data, $condition = array()) {
		if(empty($condition)){
			show_error('the condition must be specific on update command');
		}
        if (!empty($condition) && is_array($condition))
            foreach ($condition as $key => $value)
                $this->db->where($key, $value);
        $this->db->update($this->table_name, $data);
    }

    function _delete($condition) {
		if(empty($condition)){
			show_error('the condition must be specific on delete command');
		}
        if (!empty($condition) && is_array($condition)) {
            foreach ($condition as $key => $value)
                $this->db->where($key, $value);                        
        }        
        return $this->db->delete($this->table_name);
    }

	function _find_row($value) {
		$this->db->where($this->primary_key, $value);
		$rows = $this->db->get($this->table_name)->result_array();
		if (!empty($rows)) {
			return $rows[0];
		}
	}
	
	function _update_row($data) {
		$this->_update($data, array($this->primary_key => $data[$this->primary_key]));
	}
	
	function _find_by_key($key){
		$row = array();
		
		$rows = $this->getAll($key );
		if(!empty($rows)){
			$row = $rows[0];
		}
		
		return $row;
		
	}
	
	function _find_page($search_key = '', $search_fields = array(), $page = 0, $page_size = 0, $order_by = '', $order = 'ASC'){
		//init
		$result = '';
		if($page_size == 0) {
			$page_size = config_item('page_size');
		}
		if(empty($search_fields)) {
			$search_fields = $this->db->list_fields($this->table_name);
		}
		
		$this->db->from($this->table_name);
		foreach($search_fields as $item){
			$this->db->or_like($item, $search_key);
		}
		if(!empty($order_by)) {
			$this->db->order_by($order_by, $order);
		}
		$this->db->limit($page_size, $page * $page_size);
		
		$query = $this->db->get();
		$result = $query->result_array();
		
		// echo $this->db->last_query();
		
		return $result;
	}
	
	function _count_all_results($search_key = '', $search_fields = array()){
		$result = 0;
		
		
		if(empty($search_fields)) {
			$search_fields = $this->db->list_fields($this->table_name);
		}
		
		$this->db->from($this->table_name);
		foreach($search_fields as $item){
			$this->db->or_like($item, $search_key);
		}
		$result = $this->db->count_all_results();
		
		return $result;
	}
	
	function _is_fkey($field){
		return !empty($this->_foreign_keys[$field]);
	}
	
	function _insert_rows($rows, $table = ''){
		if(!empty($table)){
			$this->table_name = $table;
		}
		foreach($rows as $row){
			$this->_insert($row);
		}
	}
	
	function _get_fkey($field){
		$fkey = array();
		
		foreach($this->_foreign_keys as $ffield => $table){
			if($ffield === $field){
				$fkey = $table;
				break;
			}
		}
		
		return $fkey;
	}
	
	function _get_model($model_path){
		
	}
}
// END Model Class

/* End of file Model.php */
/* Location: ./system/core/Model.php */